<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ForetellTypeUtil
 *
 * @author Sorasak Siangsanan
 */
class ForetellTypeUtil {

    const MAXIMUM_OPTION_TYPE = 3;

    public static function getCheckbox($form, $model) {
        $options = array();
        $isStarted = false;
        $count = 0;
        for ($i = 1; $i <= 5; $i++) {
            $open = '<li><figure class="group"><figcaption class="wide-line">';
            $closed = '</figcaption></figure></li>';
            if (empty($options[$count])) {
                $options[$count] = "";
            }
            if (($count % 1) == 0 && $isStarted) {
                $options[$count] .= "<br/>";
                $options[$count] .= $closed;
                $isStarted = false;
            }
            if (($count % 1) == 0 || $count == 0) {
                $options[$count] .= $open;
                $isStarted = true;
            }
            $options[$count] .= $form->checkBox($model, 'foretell_type_' . $i, array('value' => $i, 'class' => 'js-switch'));
            $options[$count] .= ' ' . $i . '. ' . ForetellTypeUtil::getForetellType($i) . '<br/><br/>';
            ++$count;
        }
        return $options;
    }

    public static function getCheckboxWithValue($form, $model, $value) {
        $option_array = explode(':', $value);
        $result = array();
        foreach ($option_array as $option) {
            array_push($result, $option);
        }

        $options = array();

        $isStarted = false;
        $count = 0;
        for ($i = 1; $i <= 5; $i++) {
            $open = '<li><figure class="group"><figcaption>';
            $closed = '</figcaption></figure></li>';
            if (empty($options[$count])) {
                $options[$count] = "";
            }
            if (($count % 5) == 0 && $isStarted) {
                $options[$count] .= "<br/>";
                $options[$count] .= $closed;
                $isStarted = false;
            }
            if (($count % 5) == 0 || $count == 0) {
                $options[$count] .= $open;
                $isStarted = true;
            }
            if (in_array($i, $result)) {
                $options[$count] .= $form->checkBox($model, 'foretell_type_' . $i, array('value' => $i, 'checked' => 'checked', 'class' => 'js-switch'));
            } else {
                $options[$count] .= $form->checkBox($model, 'foretell_type_' . $i, array('value' => $i, 'class' => 'js-switch'));
            }
            $options[$count] .= ' ' . $i . '. ' . ForetellTypeUtil::getForetellType($i) . '<br/><br/>';
            ++$count;
        }

//        for ($i = 1; $i <= 21; $i++) {
//            if (in_array($i, $result)) {
//                $options[$i] = $form->checkBox($model, 'foretell_type_' . $i, array('value' => $i, 'checked' => 'checked'));
//            } else {
//                $options[$i] = $form->checkBox($model, 'foretell_type_' . $i, array('value' => $i));
//            }
//        }
        return $options;
    }

    public static function getDropdown() {
        $options = array();
        for ($i = 1; $i <= 5; $i++) {
            $options[$i] = self::getForetellType($i);
        }
        return $options;
    }

    public static function getForetellType($option) {
        return Yii::t('foretell_type', 'opt_' . $option);
    }

    public static function getForetellArrayType($options) {
        $option_array = explode(':', $options);
        $result = array();
        foreach ($option_array as $option) {
            array_push($result, self::getForetellType($option));
        }
        return $result;
    }

    public static function printForetellArrayType($array) {
        for ($i = 0; $i < count($array); $i++) {
            echo $i + 1 . '. ' . $array[$i] . '<br/>';
        }
    }

}
